package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.product.ProductBrandPageRequest;
import com.mes.cloud.api.dto.request.product.ProductBrandRequest;
import com.mes.cloud.api.dto.request.product.ProductBrandVoRequest;
import com.mes.cloud.api.dto.request.product.ProductCustomerBrandRequest;
import com.mes.cloud.api.dto.response.product.ProductBrandDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductBrandDao;
import com.mes.cloud.entity.ProductBrand;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品品牌
 *
 * @author ysd
 * @email shaodong.yuan@pactera.com
 * @version 2019-08-20 15:48:07
 */
@Service
public class ProductBrandService {

    private ProductBrandDao productBrandDao;

    private IPKeyGenerator iPKeyGenerator;

    private SysCodeGeneratorService sysCodeGeneratorService;


    @Autowired
    public ProductBrandService(ProductBrandDao productBrandDao,
                               IPKeyGenerator iPKeyGenerator,
                               SysCodeGeneratorService sysCodeGeneratorService){
        this.productBrandDao = productBrandDao;
        this.iPKeyGenerator = iPKeyGenerator;
        this.sysCodeGeneratorService = sysCodeGeneratorService;
    }

    /**
    * 分页查列表
    * @param pageRequest
    * @return JsonResult
    */
    public JsonResult<CommPager<ProductBrandDto>> pageList(ProductBrandPageRequest pageRequest) {
        JsonResult<CommPager<ProductBrandDto>> jsonResult = new JsonResult<>();
        Page<ProductBrandDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        List<ProductBrand> productBrandList = productBrandDao.findPageBrand(pageRequest);

        if (productBrandList != null && productBrandList.size() > 0){
            List<ProductBrandDto> productBrandDtoList = new ArrayList<>();
            for (ProductBrand productBrand: productBrandList){
                ProductBrandDto productBrandDto = new ProductBrandDto();
                BeanUtils.copyProperties(productBrand,productBrandDto);
                productBrandDtoList.add(productBrandDto);
            }
            CommPager<ProductBrandDto> pageResult = new CommPager<>(page,productBrandDtoList);

            return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        }
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(null);
    }

    /**
    * 代理能力_关联品牌
    * @param productCustomerBrandRequest
    * @return JsonResult
    */
    public JsonResult<List<ProductBrandDto>> findAllCustomerBrand(ProductCustomerBrandRequest productCustomerBrandRequest) {
        //查询条件
        List<ProductBrand> productBrandList = productBrandDao.findAllCustomerBrand(productCustomerBrandRequest);
        List<ProductBrandDto> productBrandDtoList = new ArrayList<>();
        if (productBrandList != null && productBrandList.size() > 0){
            for (ProductBrand productBrand: productBrandList){
                ProductBrandDto productBrandDto = new ProductBrandDto();
                BeanUtils.copyProperties(productBrand,productBrandDto);
                productBrandDtoList.add(productBrandDto);
            }
        }
        JsonResult<List<ProductBrandDto>> jsonResult = new JsonResult<>();
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productBrandDtoList);
    }

    /**
    * 查列表
    * @param productBrandRequest
    * @return JsonResult
    */
    public JsonResult<List<ProductBrandDto>> list(ProductBrandVoRequest productBrandRequest) {
        //查询条件
        List<ProductBrand> productBrandList = productBrandDao.findAllSelect(productBrandRequest);//sql

        List<ProductBrandDto> productBrandDtoList = new ArrayList<>();
        for (ProductBrand productBrand: productBrandList){
            ProductBrandDto productBrandDto = new ProductBrandDto();
            BeanUtils.copyProperties(productBrand,productBrandDto);
            productBrandDtoList.add(productBrandDto);
        }
        JsonResult<List<ProductBrandDto>> jsonResult = new JsonResult<>();
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productBrandDtoList);
    }

    /**
    *  新增
    * @param productBrandRequest
    * @return JsonResult
    */
    public JsonResult add(ProductBrandRequest productBrandRequest) {
        JsonResult jsonResult = new JsonResult();

        //品牌名称唯一校验
        ProductBrand selectParem = new ProductBrand();
        selectParem.setName(productBrandRequest.getName());
        int selectCount = productBrandDao.selectCount(selectParem);
        if (selectCount > 0 ){
            return jsonResult.setMsg(HttpStatusEnum.PRODUCT_BRAND_NAME_ERROR.getMessage()).setCode(HttpStatusEnum.PRODUCT_BRAND_NAME_ERROR.getCode());
        }

        ProductBrand productBrand = new ProductBrand();
        BeanUtils.copyProperties(productBrandRequest,productBrand);
        productBrand.setId(iPKeyGenerator.getGenerateKey());
        productBrand.setCode(sysCodeGeneratorService.getCode(Constants.PRODUCT_BRAND_PREFIX));
        productBrand.setUpdateDateStr(DateUtil.updateDateStr());
        productBrand.setCreateBy(UserUtil.getCreateByName());
        productBrand.setUpdateBy(UserUtil.getUpdateByName());
        productBrand.setCreateByNo(UserUtil.getCreateByNo());
        productBrand.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productBrandDao.insertSelective(productBrand);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
    * 更新
    * @param productBrandRequest
    * @return JsonResult
    */
    public JsonResult update(ProductBrandRequest productBrandRequest) {
        JsonResult jsonResult = new JsonResult();
        ProductBrand productBrand = new ProductBrand();
        ProductBrand productBrand2 = new ProductBrand();

        BeanUtils.copyProperties(productBrandRequest,productBrand);

        productBrand2.setName(productBrand.getName());
        int count2 = productBrandDao.selectCount(productBrand2);
        //品牌名重复
        if (count2 > 1 ){
            jsonResult.setMsg(HttpStatusEnum.PRODUCT_BRAND_NAME_ERROR.getMessage()).setCode(HttpStatusEnum.PRODUCT_BRAND_NAME_ERROR.getCode());
            return jsonResult;
        }

        productBrand.setUpdateDateStr(DateUtil.updateDateStr());
        productBrand.setUpdateBy(UserUtil.getUpdateByName());
        productBrand.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productBrandDao.updateByPrimaryKeySelective(productBrand);

        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        }else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }


    /**
    * 查看详情
    * @param id
    * @return JsonResult
    */
    public JsonResult<ProductBrandDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        ProductBrand productBrand= productBrandDao.selectByPrimaryKey(id);
        if(productBrand == null){
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        if (!StringUtils.isEmpty(productBrand.getBrandLogoKey())){

        }
        ProductBrandDto productBrandDto = new ProductBrandDto();
        BeanUtils.copyProperties(productBrand, productBrandDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productBrandDto);
    }
}
